package com.zeny.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeny.edu.eduservice.entity.EduCourse;
import com.zeny.edu.eduservice.entity.EduTeacher;
import com.zeny.edu.eduservice.entity.query.QueryTeacher;
import com.zeny.edu.eduservice.mapper.EduTeacherMapper;
import com.zeny.edu.eduservice.service.EduCourseService;
import com.zeny.edu.eduservice.service.EduTeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author zeny
 * @since 2020-02-24
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 条件分页查询
     * @param eduTeacherPage   分页
     * @param queryTeacher     条件
     * @return
     */
    @Override
    public IPage<EduTeacher> pageListCondition(Page<EduTeacher> eduTeacherPage, QueryTeacher queryTeacher) {
        if (queryTeacher == null) {
            return baseMapper.selectPage(eduTeacherPage, null);
        }
        QueryWrapper<EduTeacher> wrapper = getQueryWrapper(queryTeacher);
        return baseMapper.selectPage(eduTeacherPage, wrapper);
    }

    @Override
    public boolean deleteById(String id) {
        int result = baseMapper.deleteById(id);
        return result > 0;
    }

    /**
     * @Description 前台分页查询讲师
     * @Date 2020/3/9 0009 18:43
     * @param
     * @param teacherPage
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> getFrontTeacherList(Page<EduTeacher> teacherPage) {
        baseMapper.selectPage(teacherPage, null);
        //每页的数据
        List<EduTeacher> records = teacherPage.getRecords();
        //总记录数
        long total = teacherPage.getTotal();
        //每页显示的记录数
        long size = teacherPage.getSize();
        //总页数
        long pages = teacherPage.getPages();
        //当前页
        long current = teacherPage.getCurrent();
        //是否有下一页
        boolean hasNext = teacherPage.hasNext();
        //是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("size",size);
        map.put("pages",pages);
        map.put("current",current);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        map.put("items",records);
        return map;
    }

    /**
     * @Description 前台根据讲师ID查询讲师主讲的课程
     * @Date 2020/3/9 0009 19:13
     * @param
     * @param id
     * @return java.util.List<com.zeny.edu.eduservice.entity.EduCourse>
     **/
    @Override
    public List<EduCourse> getCourseOfTeacherByTeacherId(String id) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", id);
        List<EduCourse> eduCourseList = eduCourseService.list(wrapper);
        return eduCourseList;
    }

    /**
     * 封装条件
     * @param queryTeacher
     * @return
     */
    private QueryWrapper<EduTeacher> getQueryWrapper(QueryTeacher queryTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryTeacher.getName())) {
            wrapper.like("name", queryTeacher.getName());
        }
        if (StringUtils.isNotBlank(queryTeacher.getLevel())) {
            wrapper.eq("level", queryTeacher.getLevel());
        }
        if (queryTeacher.getBegin() != null) {
            wrapper.ge("gmt_create", queryTeacher.getBegin());
        }
        if (queryTeacher.getEnd() != null) {
            wrapper.le("gmt_create", queryTeacher.getEnd());
        }
        return wrapper;
    }


}
